package com.example.sensorex;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class OrientationActivity extends Activity implements SensorEventListener {

	SensorManager manager;
	TextView xValue, yValue, zValue;
	Sensor orientationSensor;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_orientation);
		
		xValue = (TextView)findViewById(R.id.xValue);
		yValue = (TextView)findViewById(R.id.yValue);
		zValue = (TextView)findViewById(R.id.zValue);
		
		manager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
		orientationSensor = manager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
	}
	
	@Override
	protected void onStart() {
		
		if(orientationSensor != null){
			manager.registerListener(this, orientationSensor, SensorManager.SENSOR_DELAY_GAME);
		}
		super.onStart();
	}
	
	@Override
	protected void onStop() {
		
		if(orientationSensor != null){
			manager.unregisterListener(this);
		}
		super.onStop();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		Intent intent = null;
		switch(id){
    	case R.id.menu_home:
    		intent = new Intent(getApplicationContext(), MainActivity.class);
    		startActivity(intent);
    		break;
    	case R.id.menu_orientaion:
    		intent = new Intent(getApplicationContext(), OrientationActivity.class);
    		startActivity(intent);
    		break;
    	case R.id.menu_balance:
    		intent = new Intent(getApplicationContext(), MainActivity.class);
    		startActivity(intent);
    		break;
    }
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	public void onSensorChanged(SensorEvent event) {
		
		Message msg = mHandler.obtainMessage();
		msg.arg1 = (int)event.values[SensorManager.DATA_X]*100;
		msg.arg2 = (int)event.values[SensorManager.DATA_Y]*100;
		msg.what = (int)event.values[SensorManager.DATA_Z]*100;
		
		mHandler.sendMessage(msg);
	}
	
	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		
		
	}
	
	Handler mHandler = new Handler(){
		public void handlemessage(Message msg){
			
			xValue.setText(""+msg.arg1);
			yValue.setText(""+msg.arg2);
			zValue.setText(""+msg.what);
			super.handleMessage(msg);
		}
	};
}
